'''
Created on 18 Oct 2009

@author: arcturus@ardeenelinfierno.com
'''
import unittest

from SnipplrPy import SnipplrPy

__all__ = ('SnipplrPyTests')
__author__ = "Francisco Jesus Jordano Jimenez"
__revision__ = 0.1


class SnipplrPyTests(unittest.TestCase):
    '''
        Unit tests for library snipplrpy.
        Will use a api key for user snipplrpy. That user is
        headless, is just for testing purposes.
        
        API Key for tests: d39322e301be5a0e64eb
    '''
    api_key = 'd39322e301be5a0e64eb'
    snippet_code = '''
import random
import unittest

class TestSequenceFunctions(unittest.TestCase):

    def setUp(self):
        self.seq = range(10)

    def testshuffle(self):
        # make sure the shuffled sequence does not lose any elements
        random.shuffle(self.seq)
        self.seq.sort()
        self.assertEqual(self.seq, range(10))

    def testchoice(self):
        element = random.choice(self.seq)
        self.assert_(element in self.seq)

    def testsample(self):
        self.assertRaises(ValueError, random.sample, self.seq, 20)
        for element in random.sample(self.seq, 5):
            self.assert_(element in self.seq)

if __name__ == '__main__':
    unittest.main()
'''
    snippet_name = "Basic python unittesting"
    snippet_tags = "python unittest"
    snippet_language = "python"

    def setUp(self):
        self.snipplrpy = SnipplrPy()
        self.snipplrpy.setup(self.api_key)

    def tearDown(self):
        self.snipplrpy = None


    def test1ValidKey(self):
        '''
            Check the api key method
        '''
        result = self.snipplrpy.check_api_key(self.api_key)
        self.assertTrue(result)
        
    def test2InvalidKey(self):
        result = self.snipplrpy.check_api_key('invalid')
        self.assertFalse(result)
        
    def test3Languages(self):
        languages = self.snipplrpy.list_languages()
        
        self.assertTrue(languages <> None)
        self.assertTrue(len(languages) > 0)
        
    def test4PostSnippet(self):
        result = self.__insertSnippet()
        
        self.assertTrue(result)
        
    def test5ListSnippets(self):
        '''
            Expect the snippet inserted in the previos
            test
        '''
        snippet_list = self.snipplrpy.list(self.snippet_tags, 'date')
        self.assertTrue(snippet_list <> None)
        self.assertTrue(len(snippet_list) >= 1)
        
        snippet = snippet_list[0]
        self.assertEqual(snippet['title'], self.snippet_name)
        
    def test6GetSnippet(self):
        id = self.__getGetLastInsertedSnippetId()
        
        code = self.snipplrpy.get(id, True)
        self.assertEquals(code['source'], self.snippet_code)
        
    def test7DeleteSnippet(self):
        id = self.__getGetLastInsertedSnippetId()
        
        result = self.snipplrpy.delete(id) 
        
        self.assertTrue(result)       
        
    def __insertSnippet(self):
        return self.snipplrpy.post(self.snippet_name, self.snippet_code, 
                                   self.snippet_tags, self.snippet_language)
        
    def __getGetLastInsertedSnippetId(self):
        snippet_list = self.snipplrpy.list(self.snippet_tags, 'date')
        snippet = snippet_list[0]
        self.assertTrue(snippet <> None)
        
        id = snippet['id']
        
        return id


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()